<template>
  <div class="app-container">
    <div v-if="crud.props.searchToggle">
      <el-input v-model="query.name" clearable size="small" placeholder="输入名称搜索" style="width: 200px;"
                class="filter-item" @keyup.enter.native="crud.toQuery"/>
      <rrOperation/>
    </div>
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission">

      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="词条(蒙)" prop="mnEntry">
            <el-input v-model="form.mnEntry" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="词条概述" prop="mnEntryOverview">
            <el-input v-model="form.mnEntryOverview" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="词条(汉)" prop="zhEntry">
            <el-input v-model="form.zhEntry" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="词条概述" prop="zhEntryOverview">
            <el-input v-model="form.zhEntryOverview" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="状态">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="form.status"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
                @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="id" label="id"/>
        <el-table-column prop="mnEntry" label="词条(蒙)"/>
        <el-table-column prop="zhEntry" label="词条(汉)"/>
        <el-table-column prop="createBy" label="创建者"/>
        <el-table-column prop="updateBy" label="修改者"/>
        <el-table-column prop="status" label="状态  ">
          <template slot-scope="scope">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              changeEnabled
              @change="changeState(scope.row,scope.row.status)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','wordArticle:edit','wordArticle:del'])" label="操作" width="150px"
                         align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>
  </div>
</template>

<script>
import crudWordArticle from '@/api/medicine/wordArticle'
import CRUD, {presenter, header, form, crud} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {getByName} from "../../../api/medicine/wordArticle";

const defaultForm = {
  id: null,
  mnEntry: null,
  mnEntryOverview: null,
  zhEntry: null,
  zhEntryOverview: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  status: null,
  remark: null
}
export default {
  name: 'WordArticle',
  components: {pagination, crudOperation, rrOperation, udOperation},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '词条管理',
      url: 'api/wordArticle',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: {...crudWordArticle}
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'wordArticle:add'],
        edit: ['admin', 'wordArticle:edit'],
        del: ['admin', 'wordArticle:del']
      },
      input: '',
      rules: {
        mnEntry: [
          {required: true, message: '请输入词条(蒙)', trigger: 'change'}
        ],
        mnEntryOverview: [
          {required: true, message: '请输入词条概述(蒙)', trigger: 'change'}
        ],
        zhEntry: [
          {required: true, message: '请输入词条(汉)', trigger: 'change'}
        ],
        zhEntryOverview: [
          {required: true, message: '请输入词条概述(汉)', trigger: 'change'}
        ],
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    changeState(data, val) {
      this.$confirm('此操作将修改状态值为 ' + val + ' 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudWordArticle.edit(data).then(res => {
          if (val === 1) {
            data.status = 0;
          } else {
            data.status = 1;
          }
          this.$message({
            type: 'waring',
            message: '修改成功'
          });
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });
    },
    search() {
      getByName(encodeURIComponent(this.input)).then(res => {
        this.crud.data = res;
      })
    }
  }
}
</script>

<style scoped>

</style>
